using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FluidDemo.Model
{
    public class Circle : IEntity
    {
        public Circle()
        {
            callback = new ParticleCallback(CallbackMethod);
        }

        Vector2 center;

        public Vector2 Center
        {
            get { return center; }
            set { center = value; }
        }

        float radius2 = 100;
        float radius = 10;

        public float Radius
        {
            get { return radius; }
            set 
            { 
                radius = value;
                radius2 = radius * radius;
            }
        }

        PhisicalEnvironment environment;

        public PhisicalEnvironment Environment
        {
            get { return environment; }
            set { environment = value; }
        }

        ParticleCallback callback;

        void CallbackMethod(Particle p) 
        {
            Vector2 d = p.Position - center;
            float dlen = d.Length();
            float r = dlen - p.Radius - radius;
            if (r > 0) return;
            Vector2 n = d; n.Normalize();
            Vector2 np = center + n * (p.Radius + radius + 1f);
            p.Position = np;
            p.Energy -= 0.1f;
        }

        public void Solve()
        {
            environment.Query.GetParticlesNear(center, new Vector2(2 * radius + 10, 2 * radius + 10), callback);
        }

        public EntityKind Kind
        {
            get { return EntityKind.Circle; }
        }
    }
}
